/****************************************************************************
*
*  ALi (Shanghai) Corporation, All Rights Reserved. 2003 Copyright (C)
*
*  File: win_ews_location_code.c
*
*  Description: 
*              
*  History:
*      Date        	Author             	Version     	Comment
*      ====        	======        	=======   	=======
*
****************************************************************************/
#include <sys_config.h>
#include <types.h>

#include <basic_types.h>
#include <mediatypes.h>
#include <api/libc/printf.h>
#include <api/libc/string.h>
#include <osal/osal.h>

#include <api/libosd/osd_lib.h>
#include <api/libchunk/chunk.h>

#include "osdobjs_def.h"

#include "string.id"
#include "images.id"
#include "osd_config.h"
#include "osd_rsc.h"

//#include "win_com_menu_define.h"
#include "win_com.h"
#include "menus_root.h"
#include "win_com_popup.h"
#include "copper_common/system_data.h"

#ifdef EWS_INDONESIA_DEMO

#include "ews_indonesia.h"

extern CONTAINER g_win_ews_location_code;

extern CONTAINER g_win_ews_location_con;
extern TEXT_FIELD ews_location_code_text;
extern EDIT_FIELD ews_location_code_edit;

static VACTION win_ews_location_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT win_ews_location_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);

static VACTION win_ews_location_item_con_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT win_ews_location_item_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);

static VACTION win_ews_location_edf_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT win_ews_location_edf_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);


#define WIN_SH_IDX	  	WSTL_WIN_BODYRIGHT_01_HD

#define CON_SH_IDX   WSTL_BUTTON_01_HD
#define CON_HL_IDX   WSTL_BUTTON_05_HD
#define CON_SL_IDX   WSTL_BUTTON_01_HD
#define CON_GRY_IDX  WSTL_BUTTON_07_HD

#define TXT_SH_IDX   WSTL_BUTTON_01_FG_HD
#define TXT_HL_IDX   WSTL_BUTTON_02_FG_HD
#define TXT_SL_IDX   WSTL_BUTTON_01_FG_HD
#define TXT_GRY_IDX  WSTL_BUTTON_07_HD

#define EDF_SH_IDX   WSTL_BUTTON_01_FG_HD
#define EDF_HL_IDX   WSTL_BUTTON_04_HD
#define EDF_SL_IDX   WSTL_BUTTON_01_FG_HD
#define EDF_GRY_IDX  WSTL_BUTTON_07_HD

/* define in win_com_menu_define.h already */
#define	W_L     	248
#define	W_T     	98
#define	W_W     	692
#define	W_H     	488

#define CON_L		(W_L+20)
#define CON_T		(W_T + 4)
#define CON_W		(W_W - 60)

//#define CON_L		W_L
#define CON_H		40
#define CON_GAP		8

#define TXT_L_OF  	10
#define TXT_W  		260
#define TXT_H		CON_H
#define TXT_T_OF	((CON_H - TXT_H)/2)

#define SEL_L_OF  	(TXT_L_OF + TXT_W)
#define SEL_W  		(CON_W-TXT_W-20)
#define SEL_H		CON_H
#define SEL_T_OF	((CON_H - SEL_H)/2)

#define LINE_L_OF	0
#define LINE_T_OF  	(CON_H+2)
#define LINE_W		CON_W
#define LINE_H     	2

#define ITEM_H          CON_H

#define LST_L		(CON_L + 10)
#define LST_T		CON_T
#define LST_W		(W_W - 40)

#define LDEF_CON(root, varCon,nxtObj,ID,IDl,IDr,IDu,IDd,l,t,w,h,hl,conobj,focusID)		\
    DEF_CONTAINER(varCon,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,IDl,IDr,IDu,IDd, l,t,w,h, CON_SH_IDX,hl,CON_SL_IDX,CON_GRY_IDX,   \
    win_ews_location_item_con_keymap,win_ews_location_item_con_callback,  \
    conobj, ID,1)


#define LDEF_EDIT(root, varNum, nxtObj, ID, l, t, w, h,style,max_len,cursormode,pat,pre,sub,str)	\
    DEF_EDITFIELD(varNum,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,ID,ID,ID,ID, l,t,w,h, EDF_SH_IDX,EDF_HL_IDX,EDF_SL_IDX,EDF_GRY_IDX,   \
    win_ews_location_edf_keymap,win_ews_location_edf_callback,  \
    C_ALIGN_CENTER | C_ALIGN_VCENTER, 30,0,style,pat,max_len,cursormode,pre,sub,str)

#define LDEF_TXT(root,varTxt,nxtObj,l,t,w,h,resID,str)		\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, TXT_SH_IDX,TXT_HL_IDX,TXT_SL_IDX,TXT_GRY_IDX,   \
    NULL,NULL,  \
    C_ALIGN_LEFT | C_ALIGN_VCENTER, 0,0,resID,str)

#define LDEF_MENU_ITEM_EDF(root,varCon,nxtObj,varTxt,varNum,ID,IDu,IDd,\
						l,t,w,h,resID,str_txt,style,max_len,cursormode,pat,pre,sub,str) \
    LDEF_CON(&root,varCon,nxtObj,ID,ID,ID,IDu,IDd,CON_L,t,CON_W,CON_H,CON_HL_IDX,&varTxt,ID)   \
    LDEF_TXT(&varCon,varTxt,&varNum,l + TXT_L_OF,t + TXT_T_OF,TXT_W,TXT_H,resID,str_txt)    \
    LDEF_EDIT(&varCon,varNum,NULL/*&varLine*/,ID,l + SEL_L_OF ,t + SEL_T_OF,SEL_W,SEL_H,style,max_len,cursormode,pat,pre,sub,str)\

#define LDEF_WIN(varCon,nxtObj,l,t,w,h,focusID)		\
    DEF_CONTAINER(varCon,NULL,NULL,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, WIN_SH_IDX,WIN_SH_IDX,WIN_SH_IDX,WIN_SH_IDX,   \
    win_ews_location_keymap,win_ews_location_callback,  \
    nxtObj, focusID,0)

static char num_pattern[] = "r1~99999";
static UINT16 ews_loaction_code_str_input[5] = {0};
static UINT16 ews_location_code_str[20]= {0};

LDEF_MENU_ITEM_EDF(g_win_ews_location_code,g_win_ews_location_con,NULL,ews_location_code_text,ews_location_code_edit,1,1,1,\
			CON_L, CON_T + (CON_H + CON_GAP)*0, CON_W, CON_H, RS_EWS_LOCATION_CODE, NULL,\
			NORMAL_EDIT_MODE, /*hilink_apn_max_len*/ 5 ,CURSOR_NO, num_pattern, NULL,NULL,ews_loaction_code_str_input)

LDEF_WIN(g_win_ews_location_code,&g_win_ews_location_con,W_L, W_T,W_W,W_H,1)


static VACTION win_ews_location_keymap(POBJECT_HEAD pObj, UINT32 key)
{
    VACTION act = VACT_PASS;
    
    switch(key)
    {
    case V_KEY_UP:
        act = VACT_CURSOR_UP;
        break;
    case V_KEY_DOWN:
        act = VACT_CURSOR_DOWN;
        break;      
    case V_KEY_EXIT:
    case V_KEY_MENU:
        act = VACT_CLOSE;
        break;
    default:    
        act = VACT_PASS;
        break;
    }
    
    return act;
}

void win_ews_location_init(void)
{
    UINT32 location_code_value = 0;

    location_code_value = sys_data_get_location_code();

    OSD_SetEditFieldContent(&ews_location_code_edit, STRING_NUMBER, location_code_value);

    return ;
}


static PRESULT win_ews_location_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
    PRESULT ret = PROC_PASS;
    VACTION unact = VACT_PASS;
    int result = 0;
    UINT32 location_code_value = 0;

    switch(event)
    {
    case EVN_PRE_OPEN:
        win_ews_location_init();
        wincom_open_title((POBJECT_HEAD)&g_win_ews_location_code,RS_EWS_LOCATION_CODE_SETTING, 0);
        break;
	case EVN_POST_OPEN:
		break;		
    case EVN_PRE_CLOSE:
        /* Make OSD not flickering */
        *((UINT32*)param2) &= ~C_CLOSE_CLRBACK_FLG; 
        break;
	case EVN_POST_CLOSE:
        OSD_GetEditFieldIntValue(&ews_location_code_edit, &location_code_value);
        sys_data_set_location_code(location_code_value);
        sys_data_save(1);     
        break;
    case EVN_MSG_GOT:
        if ((CTRL_MSG_SUBTYPE_CMD_EXIT == param1) || (CTRL_MSG_SUBTYPE_CMD_EXIT_ALL == param1))
        {
            ret = PROC_LEAVE;
        }    
        break;
    case EVN_UNKNOWN_ACTION:
        break;
    default:
        break;
    }

    return ret; 
}

static VACTION win_ews_location_item_con_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act = VACT_PASS;
	switch(key)
	{
	#if 0
	case V_KEY_ENTER:     
		act = VACT_ENTER;
		break; 
    #endif    
	default:
		act = VACT_PASS;
	}

	return act;
}

static PRESULT win_ews_location_item_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
    PRESULT ret = PROC_PASS;
    VACTION unact = VACT_PASS;

	switch(event)
	{
	#if 0
    	case EVN_UNKNOWN_ACTION:
    		unact = (VACTION)(param1 >> 16);
    		break;
    #endif        
    	default:
    		break;
	}
    return ret;
}

static VACTION win_ews_location_edf_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act = VACT_PASS;
	
	switch(key)
    {
	case V_KEY_0:	case V_KEY_1:	case V_KEY_2:	case V_KEY_3:
	case V_KEY_4:	case V_KEY_5:	case V_KEY_6:	case V_KEY_7:
	case V_KEY_8:	case V_KEY_9:
		act = key - V_KEY_0 + VACT_NUM_0;
		break;
	case V_KEY_LEFT:
		act = VACT_EDIT_LEFT;
		break;
	case V_KEY_RIGHT:
		act = VACT_EDIT_RIGHT;
		break;		       
	default:					
		act = VACT_PASS;
        break;
	}

	return act;	 
}


static PRESULT win_ews_location_edf_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	UINT8 back_save = 0;

#if 0    
    UINT8 bID = 0;
	EDIT_FIELD* edf = NULL;
    UINT32 location_code_value = 0;

	bID = OSD_GetObjID(pObj);

	edf = (EDIT_FIELD*)pObj;
#endif

	switch(event)
	{
	case EVN_KEY_GOT:
		break;
		
	case EVN_PRE_CHANGE:
		break;
    #if 0    
	case EVN_POST_CHANGE:
        libc_printf("sdfafefadgsadga++++++++++++++++");     
		break;
	case EVN_UNKNOWNKEY_GOT:
        libc_printf("sdfafefadgsadga------------------");
        if(1 == bID)
        {
            OSD_GetEditFieldIntValue(edf, &location_code_value);
            sys_data_set_location_code(location_code_value);
            sys_data_save(1);
        }
		break;		
    #endif    
	case EVN_DATA_INVALID:		
		win_compopup_init(WIN_POPUP_TYPE_OK);
        win_compopup_set_msg("Invalid number!", NULL, 0);
		win_compopup_open_ext(&back_save);
		ret = PROC_LOOP;
		break;
	}
	return ret;
}


#endif
